﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebShop.DataService;

namespace WebShop.Classes.DataSource.Driver
{
    /// <summary>
    /// Datasouce, what work with real wcf service 
    /// </summary>
    public class Wcf : IDataSource
    {
        private DataService.AppServiceClient m_service;
        private string m_language;

        public Wcf(string language)
        {
            m_service = new DataService.AppServiceClient();
            m_language = language;
        }

        public List<DataService.CategoryView> GetCategories(int parentId = 0)
        {
            return m_service.GetCategories(m_language, parentId);
        }

        public List<ProductView> GetCategoryProducts(int categoryId, int page = 0, ProductSort sort = ProductSort.NameAsc, Dictionary<ProductFilter, object> filter = null, int placeid = 0)
        {
            return m_service.GetProductsOnPage(categoryId, m_language, sort, page, ParameterConstants.ItemsOnPage, filter);
        }

        public int GetCategoryPages(int categoryId)
        {
            int? count = m_service.GetCountOfCategoryPages(categoryId, ParameterConstants.ItemsOnPage);
            return count.HasValue?count.Value:0;
        }


        public ProductFull GetProduct(long id)
        {
            return m_service.GetProductFullById(id, m_language);
        }


        public Dictionary<string, string> GetLanguages()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            result.Add("rus", "Русский");
            result.Add("eng", "English");

            return result;
        }


        public User GetUser(string login, string password)
        {
            return m_service.Login(login, password, null);
        }


        public User SetUser(User user)
        {
            return m_service.SetUser(user);
        }


        public User GetUser(long userid)
        {
            return m_service.GetUser(userid);
        }


        public List<PlaceView> GetPlaces()
        {
            List<PlaceView> result = new List<PlaceView>();
            List<PlaceView> list = m_service.GetPlaces(m_language);

            foreach (PlaceView item in list)
            {
                if (item.Id > 0)
                {
                    result.Add(item);
                }
            }
            
            return result;
        }


        public OrderFull SetOrder(OrderFull order)
        {
            long customerId = ConfigStorage.User.Id;

            Dictionary<long, int> positions = new Dictionary<long,int>();
            foreach(OrderPosition item in order.Composition) {
                positions.Add(item.Product.Id, item.Count);
            }

            int placeId = order.PlaceId;

            return m_service.SetOrder(customerId, positions, placeId, null);
        }


        public List<OrderView> GetOrders()
        {
            return m_service.GetOrderHistory(ConfigStorage.User.Id, ParameterConstants.ItemsOnHistory);
        }


        public OrderFull GetOrder(long id)
        {
            return m_service.GetOrder(id, m_language);
        }


        public CategoryView GetCategory(int id)
        {
            return m_service.GetCtegoryById(id, m_language);
        }
    }
}